const Response = require("../../class/Response");
const LocalEvent = require("../../class/LocalEvent");

WSS.get('/bds/playerLogin', async (thisClient, req, res)=>{
    if (thisClient.userType!='bds'){return;}
    let loginSuccess = WSS.clientsManager.playerLogin(thisClient, req.json.body.data);

    res = Response.replyTo(req).setUrl('/bds/playerLogin/result');

    if (loginSuccess){
        WSS.clientsManager.broadcastText(`§e${req.data.realName} 连接服务器 §7| ${thisClient.name}`, [thisClient.id]);
        WSS.clientsManager.broadcastLogger(`${req.data.realName} 连接服务器 | ${thisClient.name}`);
        WSS.logger.info(`${req.data.realName} 连接服务器 | ${thisClient.name}`);
        
        thisClient.sendJson(
            res.setStatusCode(0)
            .setStatusMessage(`${req.data.realName} 登记成功`)
            .setData(req.body.data)
        );

        WSS.clientsManager.broadcastJson(
            new Response().setUrl(CONSTANTS.EVENT.PLAYER_LOGIN)
            .setSourceId(thisClient.id)
            .setSourceBdsUuid(thisClient.bds.uuid)
            .setSourceBdsServerName(thisClient.name)
            .setStatusCode(0)
            .setStatusMessage(`${req.data.realName} 登记成功`)
            .setData(req.body.data)
        );

        WSS.api.listener.emit(CONSTANTS.EVENT.PLAYER_LOGIN, new LocalEvent()
            .setSourceId(thisClient.id)
            .setSourceBdsUuid(thisClient.bds.uuid)
            .setSourceBdsServerName(thisClient.name)
            .setStatusCode(0)
            .setStatusMessage(`${req.data.realName} 登记成功`)
            .setData(req.body.data).json
        );
        
    }else{
        WSS.clientsManager.broadcastText(`§e${req.data.realName} 登录失败, 不允许多设备登录 §7|  ${thisClient.name}`);
        WSS.clientsManager.broadcastLogger(`${req.data.realName} 登录失败, 不允许多设备登录 | ${thisClient.name}`,'warn');
        WSS.logger.warn(`${req.data.realName} 登录失败, 不允许多设备登录 | ${thisClient.name}`);
        
        thisClient.sendJson(
            res.setStatusCode(1)
            .setStatusMessage("玩家登记失败, 不允许多设备登录")
            .setData(req.body.data)
        );
    }

    // 更新客户端的在线玩家信息(一共四处, bds连接断连，玩家登录退出)
    if (loginSuccess){
        WSS.clientsManager.broadcastJson(new Response().setUrl('/bds/onlineInfo')
            .setSourceId(thisClient.id)
            .setSourceBdsUuid(thisClient.bds.uuid)
            .setSourceBdsServerName(thisClient.name)
            .setStatusCode(0)
            .setStatusMessage("更新客户端的在线玩家信息")
            .setData({
                onlineClientsInfo: WSS.clientsManager.getOnlineClientsInfo(),
                onlinePlayersInfo: WSS.clientsManager.onlinePlayers
            })
        );
    }    
});

WSS.get('/bds/playerLogout', async (thisClient, req, res)=>{
    if (thisClient.userType!='bds'){return;}
    let logoutSuccess = WSS.clientsManager.playerLogout(thisClient, req.json.body.data);

    res = Response.replyTo(req).setUrl('/bds/playerLogout/result');
    if (logoutSuccess){
        WSS.clientsManager.broadcastText(`§e${req.data.realName} 退出服务器 §7| ${thisClient.name}`, [thisClient.id]);
        WSS.clientsManager.broadcastLogger(`${req.data.realName} 退出服务器 | ${thisClient.name}`);
        WSS.logger.info(`${req.data.realName} 退出服务器 | ${thisClient.name}`);
        
        thisClient.sendJson(
            res.setStatusCode(0)
            .setStatusMessage(`${req.data.realName} 登出成功`)
            .setData(req.body.data)
        );

        WSS.clientsManager.broadcastJson(
            new Response().setUrl(CONSTANTS.EVENT.PLAYER_LOGOUT)
            .setSourceId(thisClient.id)
            .setSourceBdsUuid(thisClient.bds.uuid)
            .setSourceBdsServerName(thisClient.name)
            .setStatusCode(0)
            .setStatusMessage(`${req.data.realName} 登出成功`)
            .setData(req.body.data)
        );

        WSS.api.listener.emit(CONSTANTS.EVENT.PLAYER_LOGOUT, new LocalEvent()
            .setSourceId(thisClient.id)
            .setSourceBdsUuid(thisClient.bds.uuid)
            .setSourceBdsServerName(thisClient.name)
            .setStatusCode(0)
            .setStatusMessage(`${req.data.realName} 登出成功`)
            .setData(req.body.data).json
        );
    }else{
        WSS.clientsManager.broadcastText(`§e${req.data.realName} 登出失败 §7|  ${thisClient.name}`);
        WSS.clientsManager.broadcastLogger(`${req.data.realName} 登出失败| ${thisClient.name}`,'warn');
        WSS.logger.warn(`${req.data.realName} 登出失败| ${thisClient.name}`);
        
        thisClient.sendJson(
            res.setStatusCode(1)
            .setStatusMessage("玩家登出失败")
            .setData(req.body.data)
        );
    }

    // 更新客户端的在线玩家信息(一共四处, bds连接断连，玩家登录退出)
    if (logoutSuccess){
        WSS.clientsManager.broadcastJson(new Response().setUrl('/bds/onlineInfo')
            .setSourceId(thisClient.id)
            .setSourceBdsUuid(thisClient.bds.uuid)
            .setSourceBdsServerName(thisClient.name)
            .setStatusCode(0)
            .setStatusMessage("更新客户端的在线玩家信息")
            .setData({
                onlineClientsInfo: WSS.clientsManager.getOnlineClientsInfo(),
                onlinePlayersInfo: WSS.clientsManager.onlinePlayers
            })
        );
    } 
});